import pandas as pd
import tools

pd.set_option('display.unicode.east_asian_width', True)

base_path = r"D:\100-Project\2024-2025-1学期安排\武昌首义学院\29101303_《通信数据运维技术实训（企业）》\报告"
docx_dir = base_path + r"\提交"
excel_file = base_path + r"\成绩提取.xlsx"


def get_score(path):
    filenames = tools.get_filenames(path)
    tools.检查路径名(filenames)
    ids = []
    names = []
    scores = []
    for filename in filenames:
        if len(filename.split('.')) < 4:
            print(filename)
            exit(0)

        id = filename.split('.')[0].strip('-').split('-')[1]
        name = filename.split('.')[0].strip('-').split('-')[2]
        score = filename.split('.')[2]
        # print(f'{id} {name} {score}')
        ids.append(id)
        # names.append(name)
        scores.append(score)

    df_new = pd.DataFrame({'学号': ids, path.split('\\')[-1]: scores})
    # df_new.set_index('学号', inplace=True)

    return df_new

def 统计重复(path):
    filenames = tools.get_filenames(path)
    tools.检查路径名(filenames)
    ids = []
    names = []
    scores = []
    for filename in filenames:
        if len(filename.split('.')) < 3:
            print(filename)
            exit(0)

        name = filename.split('.')[0].strip('-').split('-')[2]
        names.append(name)

    se = pd.Series(names)
    print(se[se.duplicated()])
    return len(se[se.duplicated()])



if __name__ == '__main__':
    if 统计重复(docx_dir) != 0:
        exit(0)

    print(tools.统计时间信息(docx_dir).values)

    df0 = pd.read_excel(base_path + r"\《数据运维实训》报告提交记录.xlsx")
    df0 = df0[['学号', '姓名']]

    df1 = get_score(docx_dir)

    df0['学号'] = df0['学号'].astype(str)
    df1['学号'] = df1['学号'].astype(str)
    df3 = pd.merge(df0, df1, how='outer')
    df3 = df3[['学号', '提交']]

    df = pd.read_excel(base_path + r"\《数据运维实训》报告提交记录.xlsx", header=0)
    df.fillna(0, inplace=True)
    df = df.iloc[:, 0:2]
    df = df.astype(str)
    df = pd.merge(df, df3, on='学号', how='outer')
    df.to_excel(excel_file)